

//查询作业列表
app.post("/ZYlist", function (req, res) {
    var param = req.body;

    //登录检测
    db.all("select * from v_zyInfo where stuID=? order by zyIndex  asc", [param.user], function (err, data) {
        if (!err && data.length != 0) {

            var rtn = {
                data: data
            };
            res.send(rtn);
        }
        else {
            res.send({ data: "ZYlist_failed" });
        }

    });
});

//判断是否是压缩文件


//压缩文件


//上传作业
app.post("/uploadZY", function (req, res) {
    var form = new formidable.IncomingForm();
    form.uploadDir = __dirname + "/works";   //设置上传文件的路径 
    form.keepExtensions = true;    //保留上传文件 时扩展名
    form.multiples = true;
    form.parse(req, function (err, fields, files) {
        if (err) { console.log(err); }
        else {
            if (fields) {
                var stuID = fields.stuID;
                var stuName = fields.stuName;
                var index = fields.index;
            }
            if (files) {
                var extname = path.parse(files.file.name).ext;
                var fsave = stuID + "_" + stuName + "_作业" + index + extname;
                //改名,将覆盖同名文件           
                fs.rename(files.file.path, __dirname + "/works/" + fsave, function (err) {
                    if (!err) {
                        //写数据库
                        var size = files.file.size;
                        var params = [index, "/sourceZY/" + encodeURI(fsave), stuID, size];
                        db.run("replace into tb_homework(hwIndex,hwPath,sid,hwSize)  values(?,?,?,?)", params, function (err) {
                            if (!err) {
                                //console.log(this);
                                if (this.changes != 0) {
                                    res.send({ msg: "OK" });
                                    console.log("【" + stuID + "-" + stuName + "】 上交作业...");
                                }
                                else {
                                    res.send({ msg: "failed" });
                                }
                            }
                            else {
                                res.send({ msg: "failed" });
                            }
                        });
                    }
                });
            }
        }
    });
});